<?php

class Application_Model_Feed
{
    public $id = -1;
    public $rss_id = 0;
    public $title;
    public $description;
    public $language;
    public $url;
    public $dateModified = 0;

    public function isUpToDate($date)
    {
        $modifDate = new Zend_Date($this->dateModified, Zend_Date::TIMESTAMP);
        return $modifDate->isEarlier($date);
    }

    public static function fromTableRow($feed)
    {
        $result = new Application_Model_Feed();
        $result->id = (int)$feed['feed_id'];
        $result->rss_id = $feed['rss_id'];
        $result->title = $feed['title'];
        $result->description = $feed['description'];
        $result->language = $feed['language'];
        $result->url = $feed['url'];
        $result->dateModified = $feed['date_modified'];
        return $result;
    }

    public static function fromXml($xmlUrl)
    {
        try
        {
            $canal = Zend_Feed_Reader::import($xmlUrl);
            $feed = null;
            if ($canal)
            {
                $feed = new Application_Model_Feed();
                $feed->rss_id = $canal->getId();
                $feed->title = $canal->getTitle();
                $feed->url = $xmlUrl;
                $feed->description = $canal->getDescription();
                $feed->language = $canal->getLanguage();
                $dateModified = $canal->getDateModified();
                if (!$dateModified)
                    $dateModified = Zend_Date::now();
                $feed->dateModified = $dateModified->toValue(Zend_Date::TIMESTAMP);
            }
            return $feed;
        }
        catch (Exception $e)
        {
            return null;
        }
    }

    /**
     * Adds a feed and its feed entries for a given account
     * @param Application_Model_Account $account The account
     * @param Application_Model_DbTable_Accounts $accounts The accounts database
     * @param Application_Model_DbTable_Feeds $feeds_db The feeds database
     * @param Application_Model_DbTable_AccountFeeds $account_feeds_db The AccountFeeds database
     * @param Application_Model_DbTable_FeedEntries $feedEntries The FeedEntries database
     * @param Application_Model_DbTable_Tokens $tokens The tokens database
     * @param Application_Model_DbTable_AccountTokens $accountTokens The AccountTokens database
     * @param Application_Model_Feed $feed The feed to insert
     */
    public static function addFeed(Application_Model_Account $account,
                                Application_Model_DbTable_Accounts $accounts,
                                Application_Model_DbTable_Feeds $feeds_db,
                                Application_Model_DbTable_AccountFeeds $account_feeds_db,
                                Application_Model_DbTable_FeedEntries $feedEntries,
                                Application_Model_DbTable_Tokens $tokens,
                                Application_Model_DbTable_AccountTokens $accountTokens,
                                Application_Model_Feed $feed)
    {
        $feedRow = $feeds_db->getByRssId($feed->rss_id);
        $feed_id = 0;
        if (!$feedRow)
        {
            $feed_id = $feeds_db->insertFeed($feed->rss_id,
                $feed->title,
                $feed->description,
                $feed->language,
                $feed->url,
                $feed->dateModified);
        }
        else
        {
            $feed_id = $feedRow['feed_id'];
        }
        $accountFeedsRow = $account_feeds_db->getById($account->id, $feed_id);
        if (!$accountFeedsRow)
        {
            $account_feeds_db->insertAccountFeed($account->id, $feed_id);
        }
    }

    private static function tokenise($document)
    {
        $document = strtolower($document);
        preg_match_all('/\w+/', $document, $matches);
        return $matches[0];
    }

    /**
     * Gets the feeds from an OPML (xml) file
     * @param string $path The path to the OPML file
     * @return array: An array containing all the Feed objects from the file
     */
    public static function getFeedsFromOpml($path)
    {
        $xml = simplexml_load_file($path);
        $result = array();
        $xPath = "//outline";
        $outlines = $xml->xpath($xPath);
        foreach ($outlines as $outline)
        {
            if (isset($outline['xmlUrl']))
            {
                $feed = null;
                $xmlUrl = (string)$outline['xmlUrl'];
                $feed = self::fromXml($xmlUrl);
                if ($feed)
                    array_push($result, $feed);
            }
        }
        return $result;
    }

    public function __destruct()
    {
        unset($this->id);
        unset($this->title);
        unset($this->description);
        unset($this->language);
        unset($this->url);
        unset($this->dateModified);
    }
}

